package com.egghiro.j2se.game.sace.model.actor.bullet.bulletshooter;

import com.egghiro.framework.j2se.game.core.model.actor.IActor2D;
import com.egghiro.j2se.game.sace.device.factory.bulletfactory.BulletFactory;
import com.egghiro.j2se.game.sace.model.actor.BulletCluster;
import com.egghiro.j2se.game.sace.param.SACEConst;

public class BulletShooter {
    /**
     * BulletShooter从本体的偏移量x
     */
    protected int shiftX;
    /**
     * BulletShooter从本体的偏移量y
     */
    protected int shiftY;
    /**
     * The shift angle from axisAngle to startAngle or endAngle。
     * The arrange will be covered is (axisAngle - shiftAngle) ~ (axisAngle + shiftAngle)
     */
    protected int shiftAngle;
    protected int coverAngle;
    /**
     * The stepAngle from one bullet to the next
     */
    protected int stepAngle;
    protected BulletFactory factory;
    
    /**
     * 单发间隔<br>
     * interval before next shoot
     */
    protected final long INTERVAL;
    
    /**
     * Delay before fire
     */
    protected long delay;

    protected BulletCluster canvasBulletCluster;
    
    /**
     * 不散射
     * @param shiftX
     * @param shiftY
     * @param canvasBulletCluster
     * @param factory
     * @param interval 单发间隔
     */
    public BulletShooter(int shiftX, int shiftY, BulletCluster canvasBulletCluster, BulletFactory factory, long interval) {
        this(shiftX, shiftY, 0, 0, canvasBulletCluster, factory, interval);
    }

    /**
     * 
     * @param shiftX
     * @param shiftY
     * @param coverAngle
     * @param stepAngle
     * @param canvasBulletCluster
     * @param factory
     * @param interval 单发间隔
     */
    public BulletShooter(int shiftX, int shiftY, int coverAngle, int stepAngle, BulletCluster canvasBulletCluster,
            BulletFactory factory, long interval) {
        this.shiftX = shiftX;
        this.shiftY = shiftY;
        this.coverAngle = coverAngle;
        this.shiftAngle = coverAngle / 2;
        this.stepAngle = stepAngle;
        this.canvasBulletCluster = canvasBulletCluster;
        this.factory = factory;
        this.INTERVAL = interval;
        this.delay = 0;
    }
    
    /**
     * 计算延迟发射
     * @param flyerX 本体的RefX
     * @param flyerY 本体的RefY
     * @param target
     * @param axisAngle 指定射击的方向(中心线)
     */
   public void act(int flyerX, int flyerY, IActor2D target, int axisAngle) {
       if (delay > 0) {
           delay -= SACEConst.SLEEP;
       } else {
           fire(flyerX, flyerY, target, axisAngle);
           nextFireDelay();
       }
   }

    /**
     * 默认实现(不指定速度)<br>
     * 没有延迟直接发射。若需要延迟，请调用act（）
     * @param flyerX 本体的RefX
     * @param flyerY 本体的RefY
     * @param target
     * @param axisAngle 指定射击的方向(中心线)
     */
    protected void fire(int flyerX, int flyerY, IActor2D target, int axisAngle){
        this.canvasBulletCluster.add(this.factory.createFlyer(flyerX + this.shiftX, flyerY + this.shiftY,axisAngle - this.shiftAngle,this.coverAngle ,this.stepAngle, target));
    }
    
    /**
     * Do the delay to next fire
     */
    protected void nextFireDelay() {
//        delay += INTERVAL;
        delay = INTERVAL;
    }

}
